// test/pages/trainingDetail/trainingDetail.js
var app = getApp()
Page({
  // 设置分享到朋友圈的标题
  onShareTimeline: function () {
    return {
      title: "智克艺术 - " + this.data.dataList.name,
      query: {
        id: this.data.id
      },
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    signList: [],
    days: 0,
    hours: 0,
    minutes: 0,
    senonds: 0,
    countDownDay: 0,
    countDownHour: 0,
    countDownMinute: 0,
    countDownSecond: 0,
    passDate: false,
    check_number:0
  },
  getData(id) {
    const that = this
    setTimeout(() => {
      wx.request({
        url: app.globalData.host + 'Train/train_details',
        data: {
          id,
          token: wx.getStorageSync('token') || '',
          type: that.data.type || ''
        },
        method: 'POST',
        success(res) {
          console.log(res)
          if (res.data.code == 2000) {
            that.countime(res.data.data.end_time)
            that.setData({
              refreshed: false,
              dataList: res.data.data,
              content: that.formartimg(res.data.data.content)
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        },
        fail(err) {
          console.log(err)
        }
      })
    }, 100);
  },
  formartimg(html) {
    let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
      match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
      return match;
    });
    newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
    return newContent;
  },
  getSignData(id) {
    const that = this
    wx.request({
      url: app.globalData.host + 'Train/get_user',
      method: 'POST',
      data: {
        id
      },
      success(res) {
        console.log(res)
        if (res.data.code == 2000) {
          let data = res.data.data
          data.data = data.data.slice(0, 3)
          console.log(data)
          that.setData({
            signList: data,
            is_show: data.is_show
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }

      }
    })
  },
  signTrain() {
    if (!this.data.type) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/personal/personal',
        })
      }, 1000);
      return
    }
    wx.navigateTo({
      url: '/test/pages/trainingSign/trainingSign?id=' + this.data.id,
    })
  },
  countime(date) {
    var totalSecond = Date.parse(new Date(date)) / 1000 - Date.parse(new Date()) / 1000;
    var interval = setInterval(function () {
      // 秒数
      var second = totalSecond;
      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var days = day.toString();
      if (days.length == 1) days = '0' + days;
      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hours = hr.toString();
      if (hours.length == 1) hours = '0' + hours;
      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minutes = min.toString();
      if (minutes.length == 1) minutes = '0' + minutes;
      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var senonds = sec.toString();
      if (senonds.length == 1) senonds = '0' + senonds;
      this.setData({
        countDownDay: days,
        countDownHour: hours,
        countDownMinute: minutes,
        countDownSecond: senonds,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        wx.showToast({
          title: '报名已结束',
          icon: 'none'
        });
        this.setData({
          countDownDay: '00',
          countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
          passDate: true
        });
      }
    }.bind(this), 1000);
  },
  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.dataList.phone,
    })
  },
  checkList() {
    if (this.data.is_show != 0) {
      wx.navigateTo({
        url: '/test/pages/trainingList/trainingList?id=' + this.data.id,
      })
    }
  },
  refresh() {
    this.getData(this.data.id)
    this.getSignData(this.data.id)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    const that = this
    that.getSignData(id)
    that.setData({
      type: wx.getStorageSync('type') || '',
      id: options.id,
      check_number:options.check_number,
      fitFoot: wx.getStorageSync('safeArea')
    })
    that.getData(id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})